package com.woniu.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.constants.ErrorConstants;
import com.woniu.entity.Nuser;
import com.woniu.exception.MyErrorException;
import com.woniu.form.EditAvatarAndNicknameForm;
import com.woniu.mapper.NuserMapper;
import com.woniu.service.NuserService;
import com.woniu.utils.ConverterUtil;
import com.woniu.utils.RedisUtil;
import com.woniu.utils.TokenUtil;
import com.woniu.vo.NuserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class NuserServiceImpl extends ServiceImpl<NuserMapper, Nuser> implements NuserService {


    @Autowired
    private NuserMapper nuserMapper;

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private RedisUtil redisUtil;



    /**
     * 获取用户信息(昵称，头像)
     * @return
     */
    @Override
    public NuserInfoVO userInfo() {
        Nuser nuser = nuserMapper.selectById(tokenUtil.getUserId());
        if (ObjectUtil.isEmpty(nuser)){
            throw new MyErrorException(ErrorConstants.USER_IS_NULL);
        }
        NuserInfoVO vo = ConverterUtil.convert(nuser, NuserInfoVO.class);
        return vo;
    }

    /**
     * 修改用户头像
     * @param json 头像url avatar
     * @return
     */
    @Override
    public boolean editAvatar(JSONObject json) {
        String avatar = json.getString("avatar");

        if (StringUtils.isEmpty(avatar)){
            throw new MyErrorException(ErrorConstants.USER_AVATAR_IS_NULL);
        }

        return nuserMapper.update(null,new LambdaUpdateWrapper<Nuser>()
                .eq(Nuser::getId,tokenUtil.getUserId())
                .set(Nuser::getAvatar,avatar)) > 0;
    }

    /**
     * 修改用户昵称
     * @param json 昵称 nickname
     * @return
     */
    @Override
    public boolean editNickname(JSONObject json) {
        String nickname = json.getString("nickname");

        if (StringUtils.isEmpty(nickname)){
            throw new MyErrorException(ErrorConstants.USER_NICKNAME_IS_NULL);
        }

        if (nickname.length() > 10){
            throw new MyErrorException(ErrorConstants.USER_NICKNAME_GT_TEN);
        }

        return nuserMapper.update(null,new LambdaUpdateWrapper<Nuser>()
                .eq(Nuser::getId,tokenUtil.getUserId())
                .set(Nuser::getNickname,nickname)) > 0;
    }

    /**
     * 修改用户昵称和头像
     * @param form 头像URL,昵称
     * @return
     */
    @Override
    public boolean editAvatarAndNick(EditAvatarAndNicknameForm form) {
        // 头像和昵称不能同时为空
        if (StringUtils.isEmpty(form.getAvatarUrl()) && StringUtils.isEmpty(form.getNickname())){
            throw new MyErrorException(ErrorConstants.USER_AVATAR_NICKNAME_IS_NULL);
        }

        return nuserMapper.update(null,new LambdaUpdateWrapper<Nuser>()
                .eq(Nuser::getId,tokenUtil.getUserId())
                .set(!StringUtils.isEmpty(form.getAvatarUrl()),Nuser::getAvatar,form.getAvatarUrl())
                .set(!StringUtils.isEmpty(form.getNickname()),Nuser::getNickname,form.getNickname())
        ) > 0;
    }

    /**
     * 根据unionid查询用户
     * @param unionid
     * @return
     */
    @Override
    public Nuser getUserByUnionid(String unionid) {
        return nuserMapper.selectOne(new LambdaQueryWrapper<Nuser>()
                .eq(Nuser::getUnionid,unionid));
    }

    /**
     * 更新公众号openid
     * @param userId 用户ID
     * @param uopenid 公众号openid
     * @return
     */
    @Override
    public boolean editUopenid(String userId,String uopenid) {
        return nuserMapper.update(null,new LambdaUpdateWrapper<Nuser>()
                .eq(Nuser::getId,userId)
                .set(Nuser::getUOpenid,uopenid)) > 0;
    }


}
